package com.linkallcloud.log.core.aop;

import java.lang.reflect.Method;
import java.util.HashMap;

import org.apache.dubbo.rpc.RpcContext;
import org.aspectj.lang.JoinPoint;

import com.linkallcloud.core.annotation.LacLog;
import com.linkallcloud.core.lang.Mirror;
import com.linkallcloud.core.lang.Strings;
import com.linkallcloud.core.service.IService;
import com.linkallcloud.log.core.domain.BusiLog;

public abstract class BusiLogAspect<T extends BusiLog> extends LogAspect<T> {

    @Override
    protected void subclassDealLogs(T operatelog, JoinPoint joinPoint, Class<?> clzz, Method method, LacLog logAnnot) {
        Mirror<?> cmirror = Mirror.me(clzz);
        // operatelog.setLogMode(LogMode.SERVICE.getCode());
        if (cmirror.isOf(IService.class)) {

            if (logAnnot != null) {// 用户没有定义，自动处理
                operatelog.setOperateDesc(
                        dealStringTtemplate(false, logAnnot.desc(), joinPoint, method, new HashMap<String, Object>() {
                            private static final long serialVersionUID = 1L;
                            {
                                put("tid", operatelog.getTid());
                            }
                        }));
            }

            if (Strings.isBlank(operatelog.getOperateDesc())) {
                operatelog.setOperateDesc("日志信息");
            }

            String clientIP = RpcContext.getServiceContext().getRemoteHost();
            operatelog.setIp(clientIP);

        }
    }

}
